{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basics and Package Structure\n",
    "\n",
    "If you're just interested in pulling data, you will primarily be using `nba_api.stats.endpoints`.\n",
    "This submodule contains a class for each API endpoint supported by stats.nba.com.\n",
    "For example, [the PlayerCareerStats class](https://github.com/swar/nba_api/blob/master/nba_api/stats/endpoints/playercareerstats.py) is initialized with a player ID and returns some career statistics for the player."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PLAYER_ID</th>\n",
       "      <th>SEASON_ID</th>\n",
       "      <th>LEAGUE_ID</th>\n",
       "      <th>TEAM_ID</th>\n",
       "      <th>TEAM_ABBREVIATION</th>\n",
       "      <th>PLAYER_AGE</th>\n",
       "      <th>GP</th>\n",
       "      <th>GS</th>\n",
       "      <th>MIN</th>\n",
       "      <th>FGM</th>\n",
       "      <th>...</th>\n",
       "      <th>FT_PCT</th>\n",
       "      <th>OREB</th>\n",
       "      <th>DREB</th>\n",
       "      <th>REB</th>\n",
       "      <th>AST</th>\n",
       "      <th>STL</th>\n",
       "      <th>BLK</th>\n",
       "      <th>TOV</th>\n",
       "      <th>PF</th>\n",
       "      <th>PTS</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>203076</td>\n",
       "      <td>2012-13</td>\n",
       "      <td>00</td>\n",
       "      <td>1610612740</td>\n",
       "      <td>NOH</td>\n",
       "      <td>20.0</td>\n",
       "      <td>64</td>\n",
       "      <td>60</td>\n",
       "      <td>1846.0</td>\n",
       "      <td>349</td>\n",
       "      <td>...</td>\n",
       "      <td>0.751</td>\n",
       "      <td>165</td>\n",
       "      <td>357</td>\n",
       "      <td>522</td>\n",
       "      <td>63</td>\n",
       "      <td>75</td>\n",
       "      <td>112</td>\n",
       "      <td>89</td>\n",
       "      <td>158</td>\n",
       "      <td>867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>203076</td>\n",
       "      <td>2013-14</td>\n",
       "      <td>00</td>\n",
       "      <td>1610612740</td>\n",
       "      <td>NOP</td>\n",
       "      <td>21.0</td>\n",
       "      <td>67</td>\n",
       "      <td>66</td>\n",
       "      <td>2358.0</td>\n",
       "      <td>522</td>\n",
       "      <td>...</td>\n",
       "      <td>0.791</td>\n",
       "      <td>207</td>\n",
       "      <td>466</td>\n",
       "      <td>673</td>\n",
       "      <td>105</td>\n",
       "      <td>89</td>\n",
       "      <td>189</td>\n",
       "      <td>109</td>\n",
       "      <td>200</td>\n",
       "      <td>1394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>203076</td>\n",
       "      <td>2014-15</td>\n",
       "      <td>00</td>\n",
       "      <td>1610612740</td>\n",
       "      <td>NOP</td>\n",
       "      <td>22.0</td>\n",
       "      <td>68</td>\n",
       "      <td>68</td>\n",
       "      <td>2455.0</td>\n",
       "      <td>642</td>\n",
       "      <td>...</td>\n",
       "      <td>0.805</td>\n",
       "      <td>173</td>\n",
       "      <td>523</td>\n",
       "      <td>696</td>\n",
       "      <td>149</td>\n",
       "      <td>100</td>\n",
       "      <td>200</td>\n",
       "      <td>95</td>\n",
       "      <td>141</td>\n",
       "      <td>1656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>203076</td>\n",
       "      <td>2015-16</td>\n",
       "      <td>00</td>\n",
       "      <td>1610612740</td>\n",
       "      <td>NOP</td>\n",
       "      <td>23.0</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>2164.0</td>\n",
       "      <td>560</td>\n",
       "      <td>...</td>\n",
       "      <td>0.758</td>\n",
       "      <td>130</td>\n",
       "      <td>497</td>\n",
       "      <td>627</td>\n",
       "      <td>116</td>\n",
       "      <td>78</td>\n",
       "      <td>125</td>\n",
       "      <td>121</td>\n",
       "      <td>148</td>\n",
       "      <td>1481</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>203076</td>\n",
       "      <td>2016-17</td>\n",
       "      <td>00</td>\n",
       "      <td>1610612740</td>\n",
       "      <td>NOP</td>\n",
       "      <td>24.0</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>2708.0</td>\n",
       "      <td>770</td>\n",
       "      <td>...</td>\n",
       "      <td>0.802</td>\n",
       "      <td>172</td>\n",
       "      <td>712</td>\n",
       "      <td>884</td>\n",
       "      <td>157</td>\n",
       "      <td>94</td>\n",
       "      <td>167</td>\n",
       "      <td>181</td>\n",
       "      <td>168</td>\n",
       "      <td>2099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>203076</td>\n",
       "      <td>2017-18</td>\n",
       "      <td>00</td>\n",
       "      <td>1610612740</td>\n",
       "      <td>NOP</td>\n",
       "      <td>25.0</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>2727.0</td>\n",
       "      <td>780</td>\n",
       "      <td>...</td>\n",
       "      <td>0.828</td>\n",
       "      <td>187</td>\n",
       "      <td>644</td>\n",
       "      <td>831</td>\n",
       "      <td>174</td>\n",
       "      <td>115</td>\n",
       "      <td>193</td>\n",
       "      <td>162</td>\n",
       "      <td>159</td>\n",
       "      <td>2110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>203076</td>\n",
       "      <td>2018-19</td>\n",
       "      <td>00</td>\n",
       "      <td>1610612740</td>\n",
       "      <td>NOP</td>\n",
       "      <td>25.0</td>\n",
       "      <td>16</td>\n",
       "      <td>16</td>\n",
       "      <td>599.0</td>\n",
       "      <td>151</td>\n",
       "      <td>...</td>\n",
       "      <td>0.787</td>\n",
       "      <td>59</td>\n",
       "      <td>143</td>\n",
       "      <td>202</td>\n",
       "      <td>78</td>\n",
       "      <td>24</td>\n",
       "      <td>43</td>\n",
       "      <td>36</td>\n",
       "      <td>44</td>\n",
       "      <td>432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   PLAYER_ID SEASON_ID LEAGUE_ID     TEAM_ID TEAM_ABBREVIATION  PLAYER_AGE  \\\n",
       "0     203076   2012-13        00  1610612740               NOH        20.0   \n",
       "1     203076   2013-14        00  1610612740               NOP        21.0   \n",
       "2     203076   2014-15        00  1610612740               NOP        22.0   \n",
       "3     203076   2015-16        00  1610612740               NOP        23.0   \n",
       "4     203076   2016-17        00  1610612740               NOP        24.0   \n",
       "5     203076   2017-18        00  1610612740               NOP        25.0   \n",
       "6     203076   2018-19        00  1610612740               NOP        25.0   \n",
       "\n",
       "   GP  GS     MIN  FGM  ...   FT_PCT  OREB  DREB  REB  AST  STL  BLK  TOV  \\\n",
       "0  64  60  1846.0  349  ...    0.751   165   357  522   63   75  112   89   \n",
       "1  67  66  2358.0  522  ...    0.791   207   466  673  105   89  189  109   \n",
       "2  68  68  2455.0  642  ...    0.805   173   523  696  149  100  200   95   \n",
       "3  61  61  2164.0  560  ...    0.758   130   497  627  116   78  125  121   \n",
       "4  75  75  2708.0  770  ...    0.802   172   712  884  157   94  167  181   \n",
       "5  75  75  2727.0  780  ...    0.828   187   644  831  174  115  193  162   \n",
       "6  16  16   599.0  151  ...    0.787    59   143  202   78   24   43   36   \n",
       "\n",
       "    PF   PTS  \n",
       "0  158   867  \n",
       "1  200  1394  \n",
       "2  141  1656  \n",
       "3  148  1481  \n",
       "4  168  2099  \n",
       "5  159  2110  \n",
       "6   44   432  \n",
       "\n",
       "[7 rows x 27 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from nba_api.stats.endpoints import playercareerstats\n",
    "\n",
    "# Anthony Davis\n",
    "career = playercareerstats.PlayerCareerStats(player_id=\"203076\")\n",
    "career.get_data_frames()[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`career`, above, is a `PlayerCareerStats` object.\n",
    "This class (and the other endpoint classes) supports several methods of accessing the data: `get_dict()`, `get_json()`, `get_data_frames()`, and more.\n",
    "`get_data_frames()` returns a list of pandas DataFrames, and when working in notebooks, this is often your best option for viewing data.\n",
    "In general, the first DataFrame in this list is the primary returned data structure and the one you'll want to look at.\n",
    "\n",
    "Almost all of the endpoint classes take at least one required argument, along with several optional ones.\n",
    "In the case of `PlayerCareerStats`, a player ID is required, but the user may also specify a league ID."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At the time of writing this notebook, these are the endpoints available:\n",
    "\n",
    "<table><tr></tr><tr><td>boxscoreadvancedv2</td><td>boxscorefourfactorsv2</td><td>boxscoremiscv2</td><td>boxscoreplayertrackv2</td></tr><tr><td>boxscorescoringv2</td><td>boxscoresummaryv2</td><td>boxscoretraditionalv2</td><td>boxscoreusagev2</td></tr><tr><td>commonallplayers</td><td>commonplayerinfo</td><td>commonplayoffseries</td><td>commonteamroster</td></tr><tr><td>commonteamyears</td><td>defensehub</td><td>draftcombinedrillresults</td><td>draftcombinenonstationaryshooting</td></tr><tr><td>draftcombineplayeranthro</td><td>draftcombinespotshooting</td><td>draftcombinestats</td><td>drafthistory</td></tr><tr><td>franchisehistory</td><td>homepageleaders</td><td>homepagev2</td><td>infographicfanduelplayer</td></tr><tr><td>leaderstiles</td><td>leaguedashlineups</td><td>leaguedashplayerbiostats</td><td>leaguedashplayerclutch</td></tr><tr><td>leaguedashplayerptshot</td><td>leaguedashplayershotlocations</td><td>leaguedashplayerstats</td><td>leaguedashptdefend</td></tr><tr><td>leaguedashptstats</td><td>leaguedashptteamdefend</td><td>leaguedashteamclutch</td><td>leaguedashteamptshot</td></tr><tr><td>leaguedashteamshotlocations</td><td>leaguedashteamstats</td><td>leaguegamefinder</td><td>leaguegamelog</td></tr><tr><td>leagueleaders</td><td>leaguestandings</td><td>playbyplay</td><td>playbyplayv2</td></tr><tr><td>playerawards</td><td>playercareerstats</td><td>playercompare</td><td>playerdashboardbyclutch</td></tr><tr><td>playerdashboardbygamesplits</td><td>playerdashboardbygeneralsplits</td><td>playerdashboardbylastngames</td><td></td></tr><tr><td>playerdashboardbyshootingsplits</td><td>playerdashboardbyteamperformance</td><td>playerdashboardbyyearoveryear</td><td>playerdashptpass</td></tr><tr><td>playerdashptreb</td><td>playerdashptshotdefend</td><td>playerdashptshots</td><td>playerfantasyprofile</td></tr><tr><td>playerfantasyprofilebargraph</td><td>playergamelog</td><td>playergamestreakfinder</td><td>playernextngames</td></tr><tr><td>playerprofilev2</td><td>playersvsplayers</td><td>playervsplayer</td><td>playoffpicture</td></tr><tr><td>scoreboard</td><td>scoreboardv2</td><td>shotchartdetail</td><td>shotchartlineupdetail</td></tr><tr><td>teamdashboardbygeneralsplits</td><td></td><td></td><td></td></tr><tr><td></td><td>teamdashboardbyshootingsplits</td><td></td><td></td></tr><tr><td>teamdashlineups</td><td>teamdashptpass</td><td>teamdashptreb</td><td>teamdashptshots</td></tr><tr><td>teamdetails</td><td>teamgamelog</td><td>teamgamestreakfinder</td><td>teamhistoricalleaders</td></tr><tr><td>teaminfocommon</td><td>teamplayerdashboard</td><td>teamplayeronoffdetails</td><td>teamplayeronoffsummary</td></tr><tr><td>teamvsplayer</td><td>teamyearbyyearstats</td><td>videodetails</td><td>videoevents</td></tr><tr><td>videostatus</td></tr></table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Getting Team and Player IDs\n",
    "The package also includes utilities for fetching player and team information available under `nba_api.stats.static`.\n",
    "You can use this to fetch player IDs and team IDs, which are often used as inputs to API endpoints."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of teams fetched: 30\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'id': 1610612737,\n",
       "  'full_name': 'Atlanta Hawks',\n",
       "  'abbreviation': 'ATL',\n",
       "  'nickname': 'Hawks',\n",
       "  'city': 'Atlanta',\n",
       "  'state': 'Atlanta',\n",
       "  'year_founded': 1949},\n",
       " {'id': 1610612738,\n",
       "  'full_name': 'Boston Celtics',\n",
       "  'abbreviation': 'BOS',\n",
       "  'nickname': 'Celtics',\n",
       "  'city': 'Boston',\n",
       "  'state': 'Massachusetts',\n",
       "  'year_founded': 1946},\n",
       " {'id': 1610612739,\n",
       "  'full_name': 'Cleveland Cavaliers',\n",
       "  'abbreviation': 'CLE',\n",
       "  'nickname': 'Cavaliers',\n",
       "  'city': 'Cleveland',\n",
       "  'state': 'Ohio',\n",
       "  'year_founded': 1970}]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from nba_api.stats.static import teams\n",
    "\n",
    "# get_teams returns a list of 30 dictionaries, each an NBA team.\n",
    "nba_teams = teams.get_teams()\n",
    "print(\"Number of teams fetched: {}\".format(len(nba_teams)))\n",
    "nba_teams[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of players fetched: 4374\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'id': 76001,\n",
       "  'full_name': 'Alaa Abdelnaby',\n",
       "  'first_name': 'Alaa',\n",
       "  'last_name': 'Abdelnaby'},\n",
       " {'id': 76002,\n",
       "  'full_name': 'Zaid Abdul-Aziz',\n",
       "  'first_name': 'Zaid',\n",
       "  'last_name': 'Abdul-Aziz'},\n",
       " {'id': 76003,\n",
       "  'full_name': 'Kareem Abdul-Jabbar',\n",
       "  'first_name': 'Kareem',\n",
       "  'last_name': 'Abdul-Jabbar'},\n",
       " {'id': 51,\n",
       "  'full_name': 'Mahmoud Abdul-Rauf',\n",
       "  'first_name': 'Mahmoud',\n",
       "  'last_name': 'Abdul-Rauf'},\n",
       " {'id': 1505,\n",
       "  'full_name': 'Tariq Abdul-Wahad',\n",
       "  'first_name': 'Tariq',\n",
       "  'last_name': 'Abdul-Wahad'}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from nba_api.stats.static import players\n",
    "\n",
    "# get_players returns a list of dictionaries, each representing a player.\n",
    "nba_players = players.get_players()\n",
    "print(\"Number of players fetched: {}\".format(len(nba_players)))\n",
    "nba_players[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To search for an individual team or player by its name (or other attribute), dictionary comprehensions are your friend."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 1610612759,\n",
       " 'full_name': 'San Antonio Spurs',\n",
       " 'abbreviation': 'SAS',\n",
       " 'nickname': 'Spurs',\n",
       " 'city': 'San Antonio',\n",
       " 'state': 'Texas',\n",
       " 'year_founded': 1976}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spurs = [team for team in nba_teams if team[\"full_name\"] == \"San Antonio Spurs\"][0]\n",
    "spurs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 1495,\n",
       " 'full_name': 'Tim Duncan',\n",
       " 'first_name': 'Tim',\n",
       " 'last_name': 'Duncan'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "big_fundamental = [\n",
    "    player for player in nba_players if player[\"full_name\"] == \"Tim Duncan\"\n",
    "][0]\n",
    "big_fundamental"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nba-api-hat5zdEy-py3.9",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
